<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i>
                    数据备份
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">

                <el-date-picker v-model="query.backUpQuery.beginDate" class="handle-select " style="margin-right: 10px"
                                type="date" placeholder="开始时间"></el-date-picker>
                <el-date-picker v-model="query.backUpQuery.endDate" class="handle-select " style="margin-right: 10px"
                                type="date" placeholder="结束时间"></el-date-picker>
                <el-input v-model="query.backUpQuery.fileName" placeholder="文件名" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch()">搜索</el-button>
                <el-button type="info" icon="el-icon-delete" @click="resetData()">清空</el-button>
            </div>
            <div class="handle-box">
                <el-button
                        type="success"
                        icon="el-icon-collection-tag"
                        class="handle-del mr4"
                        @click="preBackUp()"
                >数据备份
                </el-button>
                <el-button
                        type="primary"
                        icon="el-icon-refresh-left"
                        class="handle-del mr4"
                        @click="recoverData()"
                >数据恢复
                </el-button>
                <el-button
                        type="danger"
                        icon="el-icon-delete"
                        class="handle-del"
                        @click="delAllSelection()"
                >批量删除
                </el-button>
                <span style="margin-left: 80px;">数据备份间隔时间：</span>
                <el-input v-model="AutoBackUpTime" placeholder="15"
                          style="display: inline-block;width: 50px;"></el-input>
                &nbsp天
                <el-button type="primary" icon="el-icon-thumb" style="margin-left: 10px" @click="autoCoverData()">保存
                </el-button>
            </div>
            <el-table
                    :data="tableData"
                    border
                    class="table"
                    ref="multipleTable"
                    header-cell-class-name="table-header"
                    @selection-change="handleSelectionChange"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="myId" label="ID" width="50" align="center">
                    <template #default="scope">
                        {{ (query.pageIndex - 1) * query.pageSize + scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="fileName" width="250" label="文件名" align="center"></el-table-column>
                <el-table-column prop="gmtCreate" width="250" label="备份时间" align="center"></el-table-column>
                <el-table-column prop="location" label="保存路径" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template #default="scope">
                        <el-button
                                type="text"
                                icon="el-icon-edit"
                                @click="handleDelete(scope.$index, scope.row)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                        background
                        layout="total, prev, pager, next"
                        :current-page="query.pageIndex"
                        :page-size="query.pageSize"
                        :total="pageTotal"
                        @current-change="handlePageChange"
                ></el-pagination>
            </div>

            <!--弹出框-->
            <el-dialog title="提示" v-model="centerDialogVisible" width="30%" center>
                <i style="color:green;font-size: 18px" class="el-icon-warning"></i><span
                    style="margin-left: 10px">请选择合适的值</span>
                <div style="text-align: center;margin-top: 30px;margin-bottom: 10px;">
                    <el-select style="width: 60%" v-model="optionsValue" placeholder="请选择合适的处理算法">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                        >
                        </el-option>
                    </el-select>
                    <el-input v-model="backUpName" :placeholder="backUpPlaceholder"
                              style="margin-top: 30px;width: 60%"></el-input>
                </div>
                <template #footer>
                        <span class="dialog-footer">
                        <el-button type="info" @click="centerDialogVisible = false">取 消</el-button>
                        <el-button class="my_button" type="primary" @click="backUp()">备 份</el-button>
                        </span>
                </template>
            </el-dialog>
        </div>

    </div>
</template>

<script>
    import api from "../../../api";

    export default {
        name: "operLog",
        data() {
            return {
                query: {
                    backUpQuery: {
                        beginDate: null,
                        endDate: null,
                        fileName: null,
                    },
                    pageIndex: 1,
                    pageSize: 8,
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                pageTotal: 0,
                centerDialogVisible: false,
                backUpName: "",
                options: [
                    {
                        value: '1',
                        label: '备份到远程（默认）',
                    },
                    {
                        value: '2',
                        label: '备份到本地',
                    }
                ],
                optionsValue: "",
                backUpPlaceholder: "",
                AutoBackUpTime: "15",
            };
        },
        watch: {
            optionsValue(newName) {
                if (newName === "1") {
                    this.backUpPlaceholder = "请输入保存文件名"
                } else {
                    this.backUpPlaceholder = "请输入备份本地的地址"
                }
            }
        },
        created() {
            this.getData();
        },
        methods: {
            //备份数据前操作初始化
            preBackUp() {
                this.backUpName = "";
                this.optionsValue = "1"
                this.centerDialogVisible = true;
                this.$message.success("同一天的文件名请不要一样，否则可能会出现覆盖现象");
            },
            //备份数据
            backUp() {
                if (this.optionsValue === "1") {
                    //备份到远程
                    if (this.backUpName === "") {
                        this.$message.warning("输入不能为空");
                        return;
                    }
                    this.$message.warning("备份时间会有点长，请您稍等或稍后刷新查看");
                    api.backUpRemote(this.backUpName).then(response => {
                        this.handleSearch();
                        this.$message.success("备份成功");
                    }).catch(() => {
                        this.$message.error("备份失败");
                    })
                } else {
                    //备份到本地
                    if (this.backUpName === "") {
                        this.$message.warning("输入不能为空");
                        return;
                    }
                    api.backUpLocal(this.backUpName).then(response => {
                        this.handleSearch();
                        this.$message.success("备份成功");
                    }).catch(() => {
                        this.$message.error("备份失败");
                    })
                }
                this.centerDialogVisible = false;
            },
            //设置自动备份恢复数据
            autoCoverData() {
                if (this.AutoBackUpTime === "") {
                    this.$message.warning("备份时间不能为空");
                    return;
                }
                this.$confirm("确定要恢复吗？", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: "warning"
                }).then(() => {
                    api.autoBackUp(this.AutoBackUpTime).then(response => {
                        this.$message.success("自动备份成功");
                    }).catch(() => {
                        this.$message.error("自动备份成失败");
                    })
                }).catch(() => {
                })
            },
            //恢复数据
            recoverData() {
                if (this.multipleSelection.length > 1) {
                    this.$message.warning("最多同时恢复一个数据集");
                    return;
                } else if (this.multipleSelection.length === 0) {
                    this.$message.warning("至少选中一行数据集");
                    return;
                }
                this.$confirm("确定要恢复吗？", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: "warning"
                }).then(() => {
                    this.$message.warning("恢复数据会有些长，请您稍等或稍后刷新查看");
                    api.recover(this.multipleSelection[0].id).then(response => {
                        this.handleSearch();
                        this.$message.success("恢复数据成功");
                    }).catch(() => {
                        this.$message.error("恢复数据失败");
                    })
                }).catch(() => {
                })
            },
            //获取初始信息
            getData() {
                api.getBackUpData(this.query.pageIndex, this.query.pageSize, this.query.backUpQuery).then(res => {
                    this.tableData = res.data.item.records;
                    this.pageTotal = res.data.item.total;
                });
                api.getTime().then(res => {
                    this.AutoBackUpTime = res.data.item
                });
            },
            //检查数据是否为空字符串
            checkDate() {
                let beginDate = this.query.backUpQuery.beginDate
                let endDate = this.query.backUpQuery.endDate
                let fileName = this.query.backUpQuery.fileName

                if (beginDate === "") {
                    this.query.backUpQuery.beginDate = null;
                }
                if (endDate === "") {
                    this.query.backUpQuery.endDate = null;
                }
                if ((beginDate === null && endDate !== null) || (beginDate !== null && endDate === null)) {
                    this.$message.warning("请把搜索需要的时间差补齐");
                    return false;
                }
                if (beginDate !== null && endDate !== null && beginDate > endDate) {
                    this.$message.warning("开始时间不得大于结束时间");
                    return false;
                }
                if (fileName === "") {
                    this.query.backUpQuery.fileName = null;
                }
                return true;
            },
            //清空数据
            resetData() {
                this.query.backUpQuery.beginDate = null;
                this.query.backUpQuery.endDate = null;
                this.query.backUpQuery.fileName = null;
            },
            // 触发搜索按钮
            handleSearch() {
                if (this.checkDate()) {
                    this.getData();
                }
            },
            // 删除操作
            handleDelete(index, row) {
                // 二次确认删除
                this.$confirm("确定要删除吗？", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: "warning"
                })
                    .then(() => {
                        let array = [];
                        array[0] = {"id": row.id};
                        api.delBackUp(array).then((resp) => {
                            this.$message.success("删除成功");
                            this.tableData.splice(index, 1);
                        }).catch(() => {
                            this.$message.error("删除失败");
                        })

                    }).catch(() => {
                })
            },
            // 多选操作
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            delAllSelection() {
                // 二次确认删除
                this.$confirm("确定要删除吗？", "提示", {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: "warning"
                })
                    .then(() => {
                        if (this.multipleSelection.length === 0){
                            this.$message.warning("请至少选择一条数据");
                            return;
                        }
                        for (let i = 0; i < this.multipleSelection.length; i++) {
                            let id = "id" + i
                            this.delList[i] = {id: this.multipleSelection[i].id};
                        }
                        api.delBackUp(this.delList).then((resp) => {
                            this.$message.success("删除成功");
                            this.multipleSelection = [];
                            this.handleSearch();
                        }).catch(() => {
                            this.$message.error("删除失败");
                        });
                    }).catch(() => {
                })
            },
            // 分页导航
            handlePageChange(val) {
                this.tableData = []
                this.query.pageIndex = val;
                this.handleSearch();
            }
        }
    };
</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-select {
        width: 120px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }


    .table {
        width: 100%;
        font-size: 14px;

    }

    .red {
        color: #ff0000;
    }

    .mr10 {
        margin-right: 10px;
    }

    .mr4 {
        margin-right: 4px;
    }

    .table-td-thumb {
        display: block;
        margin: auto;
        width: 40px;
        height: 40px;
    }
</style>
